package ru.gerunn.test.jndi;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;

public class CloseSafeMemoryContextFactory implements InitialContextFactory {

	private static final String SHARE_DATA_PROPERTY = "org.osjava.sj.jndi.shared";

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Context getInitialContext(Hashtable environment) throws NamingException {

		// clone the environnement
		Hashtable sharingEnv = (Hashtable) environment.clone();

		// all instances will share stored data
		if (!sharingEnv.containsKey(SHARE_DATA_PROPERTY)) {
			sharingEnv.put(SHARE_DATA_PROPERTY, "true");
		}

		return new CloseSafeMemoryContext(sharingEnv);
	}

}